import { validateNull, isBlank } from "@/utils/validate";
import printJS from "print-js";

/**
 * 流程定义工具类
 * @returns
 */

//获取随机节点id
export function getRandNodeId() {
  //前缀node_ + 时间戳 + 4位随机数
  return `${new Date().getTime()}${Math.floor(Math.random() * 10000)
    .toString()
    .padStart(4, "0")}`;
}

export function generateNumber(len: number) {
  //前缀node_ + 时间戳 + 4位随机数
  let result = "";
  const characters = "0123456789";
  let charactersLength = characters.length;
  for (let i = 0; i < len; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

export function generateStr(len: number) {
  let result = "";
  const characters = "abcdefghijklmnopqrstuvwxyz0123456789";
  let charactersLength = characters.length;
  for (let i = 0; i < len; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

export function deepCopy(obj) {
  return JSON.parse(JSON.stringify(obj));
}

export function delField(cols, i) {
  cols.splice(i, 1);
}

export function copyField(cols, i) {
  const col = deepCopy(cols[i]);
  col.id = getRandNodeId();
  col.code = col.type + getRandNodeId();
  cols.push(col);
}

//重载所有的节点id
export function reloadNodeId(nodes) {
  if (Array.isArray(nodes)) {
    nodes.forEach(node => {
      if (node.type === "GATEWAY") {
        node.id = getRandNodeId();
        //分支头部节点
        reloadNodeId(node.props.branch);
        //分支
        reloadNodeId(node.branch);
      } else {
        node.id = getRandNodeId();
      }
    });
  } else {
    nodes.id = getRandNodeId();
  }
}

export function parseWithFunctions(obj: any, _this?) {
  if (!_this) {
    return parseWithString(obj);
  }
  return JSON.parse(obj, (key, val) => {
    if (key === "onLoad" || key === "uploadPreview") {
      return new Function(`return ${val};`).apply(_this);
    }
    return val;
  });
}

export function parseWithString(obj: string) {
  if (typeof obj === "string" && !isBlank(obj)) {
    return JSON.parse(obj);
  } else {
    return obj;
  }
}

export const printHtml = (id: string, header: string, data?: any) => {
  setTimeout(() => {
    printJS({
      type: "html",
      printable: id,
      header: header,
      targetStyles: ["*"],
      scanStyles: false
    });
    if (data) data.width = "-";
  }, 100);
};

export function setPropsNull(form: any, ...keys: string[]) {
  if (arguments.length < 2) return;
  for (let i = 0; i < keys.length; i++) {
    form[keys[i]] = null;
  }
}

// 统一设置属性值
// @ts-ignore
export function setPropsValue(form: any, value: any) {
  if (arguments.length < 3) return;
  const args = Array.prototype.slice.call(arguments);
  for (let i = 2; i < args.length; i++) {
    form[args[i]] = value;
  }
}

// 设置属性为data值
// @ts-ignore
export function setPropsDataValue(form, data) {
  if (arguments.length < 3) {
    const keys = Object.keys(data);
    for (let i = 2; i < keys.length; i++) {
      if (!validateNull(data[keys[i]])) form[keys[i]] = data[keys[i]];
    }
    return;
  }
  const args = Array.prototype.slice.call(arguments);
  // 第三个参数是否为数组
  if (args[2] instanceof Array) {
    args[2].forEach(key => {
      form[key] = data[key];
    });
  } else {
    for (let i = 2; i < args.length; i++) {
      if (!validateNull(data[args[i]])) form[args[i]] = data[args[i]];
    }
  }
}
